export const decodeClear = (vals: string) => {
  if (!vals || vals.length < 2) return [];

  const value = vals;
  const len = 6;
  const isNotLeng = value.length % len !== 0 || (value !== '' && value.length < len);
  if (!value || isNotLeng) return [];
  const valueStr: any = value.match(/[0-9|a-f|A-F]{6}/g);

  return valueStr.map((val, index) => {
    const hour = parseInt(val.slice(0, 2), 16);

    const minute = parseInt(val.slice(2, 4), 16);

    const status = parseInt(val.slice(4, 6), 16) === 1;

    return {
      hour,
      minute,
      status,
      index,
    };
  });
};

export const encodeClear = (
  data: { hour: number; minute: number; status: boolean; index?: number }[]
) => {
  if (!data.length) return '00';
  return data
    .map(item => {
      const hour = item.hour.toString(16).padStart(2, '0');
      const minute = item.minute.toString(16).padStart(2, '0');
      const status = item.status ? '01' : '00';
      return `${hour}${minute}${status}`;
    })
    .join('');
};

// 0b 09 000f 01

// 0b09 000f 01
// 0000 0064 01

export const decodeDisinfect = (
  vals: string
): { hour: number; minute: number; status: boolean; index: number; duration: number }[] => {
  if (!vals || vals.length < 2) return [];

  const value = vals;
  const len = 10;
  const isNotLeng = value.length % len !== 0 || (value !== '' && value.length < len);
  if (!value || isNotLeng) return [];
  const valueStr: any = value.match(/[0-9|a-f|A-F]{10}/g);

  return valueStr.map((val, index) => {
    const hour = parseInt(val.slice(0, 2), 16);

    const minute = parseInt(val.slice(2, 4), 16);

    const duration = parseInt(val.slice(4, 8), 16);

    const status = parseInt(val.slice(8, 10), 16) === 1;

    return {
      hour,
      minute,
      status,
      index,
      duration,
    };
  });
};

export const encodeDisinfect = (
  data: { hour: number; minute: number; status: boolean; index?: number; duration?: number }[]
) => {
  if (!data.length) return '00';
  return data
    .map(item => {
      const hour = item.hour.toString(16).padStart(2, '0');
      const minute = item.minute.toString(16).padStart(2, '0');
      const duration = item.duration?.toString(16).padStart(4, '0') || '0000';
      const status = item.status ? '01' : '00';
      return `${hour}${minute}${duration}${status}`;
    })
    .join('');
};

export const decodeDisturb = (val: string) => {
  if (val.length < 8)
    return {
      status: 0,
      starthour: 0,
      startminute: 0,
      endhour: 0,
      endminute: 0,
    };
  const starthour = parseInt(val.slice(0, 2), 16);
  const startminute = parseInt(val.slice(2, 4), 16);

  const endhour = parseInt(val.slice(4, 6), 16);
  const endminute = parseInt(val.slice(6, 8), 16);

  const status = parseInt(val.slice(8, 10), 16);

  return {
    status,
    starthour,
    startminute,
    endhour,
    endminute,
  };
};

export const encodeDisturb = (data: {
  status: number;
  starthour: number;
  startminute: number;
  endhour: number;
  endminute: number;
}) => {
  const starthour = data.starthour.toString(16).padStart(2, '0');
  const startminute = data.startminute.toString(16).padStart(2, '0');
  const endhour = data.endhour.toString(16).padStart(2, '0');
  const endminute = data.endminute.toString(16).padStart(2, '0');
  const status = data.status.toString(16).padStart(2, '0');
  return `${starthour}${startminute}${endhour}${endminute}${status}`;
};
